﻿Imports System.Collections.Generic

Partial Class WebForm_CommonControl_SearchEmployControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = False

        If Not IsPostBack Then
            Me.BindDDLOrganization()
        End If
    End Sub

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        RaiseEvent Saving(Me, New EventArgs)
    End Sub

    Public Sub Show()
        Me.mpSearch.Show()
        Me.Visible = True
        Me.BindGV()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
        Me.Show()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
        Me.Show()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If CType(e.Row.FindControl("lblRoleID"), Label).Text <> "" Then
                CType(e.Row.FindControl("chkUserID"), CheckBox).Enabled = False
            End If
        End If
    End Sub
End Class

Partial Class WebForm_CommonControl_SearchEmployControl
#Region "資料繫結"

    Private Sub BindGV()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select e.*, o.OrganizationName,r.RoleID ")
        sql.AppendFormat(" From Employee e Inner join Organization o ON e.OrganizationID = o.OrganizationID ")
        sql.AppendFormat(" Left join ( Select * From RoleUserMapping Where RoleID=@RoleID ) r ON e.EmployeeID = r.UserID ")
        sql.AppendFormat(" Where 1=1 ")
        sql.AddWithValue("RoleID", Me.HasRoleID)

        If Me.ddlOrganization.SelectedValue <> "" Then
            sql.AppendFormat(" And e.OrganizationID = @OrganizationID ")
            sql.AddWithValue("OrganizationID", Me.ddlOrganization.SelectedValue)
        End If

        If Me.txtUserID.Text <> "" Then
            sql.AppendFormat(" And e.EmployeeID = @EmployeeID ")
            sql.AddWithValue("EmployeeID", Me.txtUserID.Text.Replace("'", "''"))
        End If

        If Me.txtUserName.Text <> "" Then
            sql.AppendFormat(" And e.EmployeeName LIKE '%{0}%' ", Me.txtUserName.Text.Replace("'", "''"))
        End If

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub

    Private Sub BindDDLOrganization()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select * from Organization Where Active='Y' ")

        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "OrganizationID"
        Me.ddlOrganization.DataSource = dat.GetData(sql)
        Me.ddlOrganization.DataBind()

        Me.ddlOrganization.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

    End Sub

#End Region

#Region "Event"
    Public Event Saving(ByVal sender As Object, ByVal e As System.EventArgs)
#End Region

#Region "Property"
    Public ReadOnly Property SelectedUserList() As List(Of String)
        Get
            Dim userList As New List(Of String)

            For Each row As GridViewRow In Me.gv.Rows
                Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

                If TryCast(row.FindControl("chkUserID"), CheckBox).Checked = True Then
                    userList.Add(key("EmployeeID"))
                End If
            Next

            Return userList
        End Get
    End Property

    Public Property HasRoleID() As Integer
        Get
            Return ViewState("HasRoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("HasRoleID") = value
        End Set
    End Property
#End Region
End Class
